package com.zp.business.impl.patient;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.patient.PatMasterIndex;
import com.zp.business.entity.patient.bo.PatMasterIndexBo;
import com.zp.business.entity.patient.vo.PatMasterIndexVo;
import com.zp.business.mapper.patient.PatMasterIndexMapper;
import com.zp.business.service.patient.IPatMasterIndexService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 病人信息表Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-12-26
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class PatMasterIndexServiceImpl implements IPatMasterIndexService {

    private final PatMasterIndexMapper baseMapper;

    /**
     * 查询病人信息表
     */
    @Override
    public PatMasterIndexVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询病人信息表列表(分页)
     */
    @Override
    public TableDataInfo<PatMasterIndexVo> queryPageList (PatMasterIndexBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PatMasterIndex> lqw = buildQueryWrapper(bo);
        Page<PatMasterIndexVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询病人信息表列表(不分页)
     */
    @Override
    public List<PatMasterIndexVo> queryList (PatMasterIndexBo bo) {
        LambdaQueryWrapper<PatMasterIndex> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PatMasterIndex> buildQueryWrapper (PatMasterIndexBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PatMasterIndex> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), PatMasterIndex::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getInputCode()), PatMasterIndex::getInputCode, bo.getInputCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), PatMasterIndex::getSex, bo.getSex());
        lqw.eq(bo.getAge() != null, PatMasterIndex::getAge, bo.getAge());
        lqw.eq(bo.getAgeMonth() != null, PatMasterIndex::getAgeMonth, bo.getAgeMonth());
        lqw.eq(bo.getAgeDay() != null, PatMasterIndex::getAgeDay, bo.getAgeDay());
        lqw.eq(bo.getAgeHours() != null, PatMasterIndex::getAgeHours, bo.getAgeHours());
        lqw.eq(bo.getAgeMinutes() != null, PatMasterIndex::getAgeMinutes, bo.getAgeMinutes());
        lqw.eq(StringUtils.isNotBlank(bo.getNation()), PatMasterIndex::getNation, bo.getNation());
        lqw.eq(StringUtils.isNotBlank(bo.getNationality()), PatMasterIndex::getNationality, bo.getNationality());
        lqw.eq(bo.getBirthdayDate() != null, PatMasterIndex::getBirthdayDate, bo.getBirthdayDate());
        lqw.eq(StringUtils.isNotBlank(bo.getWeight()), PatMasterIndex::getWeight, bo.getWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getMarriage()), PatMasterIndex::getMarriage, bo.getMarriage());
        lqw.eq(StringUtils.isNotBlank(bo.getNativePlaceCode()), PatMasterIndex::getNativePlaceCode, bo.getNativePlaceCode());
        lqw.eq(StringUtils.isNotBlank(bo.getNativePlace()), PatMasterIndex::getNativePlace, bo.getNativePlace());
        lqw.eq(StringUtils.isNotBlank(bo.getZipCode()), PatMasterIndex::getZipCode, bo.getZipCode());
        lqw.eq(StringUtils.isNotBlank(bo.getHomePlaceCode()), PatMasterIndex::getHomePlaceCode, bo.getHomePlaceCode());
        lqw.eq(StringUtils.isNotBlank(bo.getHomePlace()), PatMasterIndex::getHomePlace, bo.getHomePlace());
        lqw.eq(StringUtils.isNotBlank(bo.getCountryGroup()), PatMasterIndex::getCountryGroup, bo.getCountryGroup());
        lqw.eq(StringUtils.isNotBlank(bo.getAddressCode()), PatMasterIndex::getAddressCode, bo.getAddressCode());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), PatMasterIndex::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getIdentity()), PatMasterIndex::getIdentity, bo.getIdentity());
        lqw.eq(StringUtils.isNotBlank(bo.getIdNo()), PatMasterIndex::getIdNo, bo.getIdNo());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), PatMasterIndex::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), PatMasterIndex::getContacts, bo.getContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getContactsAddress()), PatMasterIndex::getContactsAddress, bo.getContactsAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getContactsPhone()), PatMasterIndex::getContactsPhone, bo.getContactsPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitInContract()), PatMasterIndex::getUnitInContract, bo.getUnitInContract());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitEin()), PatMasterIndex::getUnitEin, bo.getUnitEin());
        lqw.eq(StringUtils.isNotBlank(bo.getPreferenceType()), PatMasterIndex::getPreferenceType, bo.getPreferenceType());
        lqw.eq(StringUtils.isNotBlank(bo.getChargeType()), PatMasterIndex::getChargeType, bo.getChargeType());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), PatMasterIndex::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getLinkfaceId()), PatMasterIndex::getLinkfaceId, bo.getLinkfaceId());
        lqw.eq(StringUtils.isNotBlank(bo.getCanBaoType()), PatMasterIndex::getCanBaoType, bo.getCanBaoType());
        return lqw;
    }

    /**
     * 新增病人信息表
     */
    @Override
    public Boolean insertByBo (PatMasterIndexBo bo) {
        return ObjectUtil.isNotNull(savePatMasterIndex(bo));
    }

    public PatMasterIndex savePatMasterIndex (PatMasterIndexBo bo) {
        PatMasterIndex add = BeanUtil.toBean(bo, PatMasterIndex.class);
        validEntityBeforeSave(add);
        add.setDelFlag("0");
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return add;
    }

    /**
     * 新增病人信息表
     *
     * @param bo
     * @return patMasterIndex 对象
     */
    @Override
    @Transactional
    public PatMasterIndex insertByBoRetPMI (PatMasterIndexBo bo) {
        return savePatMasterIndex(bo);
    }

    /**
     * 修改病人信息表
     */
    @Override
    public Boolean updateByBo (PatMasterIndexBo bo) {
        PatMasterIndex update = BeanUtil.toBean(bo, PatMasterIndex.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (PatMasterIndex entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除病人信息表
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
